package webapp;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class UserPanel
 */
public class UserPanel extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserPanel() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("We are in Service method of UserPanel");
		PrintWriter out=response.getWriter(); 
		
		HttpSession session=request.getSession(false); 
		String un = (String) session.getAttribute("name");
		
        if(un!=null){
		
		String action = request.getParameter("action");
		

		if ("Add a Purchase".equals(action)) {
		    // Invoke FirstServlet's job here.
			response.sendRedirect("Purchase.jsp");
		} else if ("Add a Sale".equals(action)) {
		    // Invoke SecondServlet's job here.
			response.sendRedirect("Sale.jsp");
		}else if ("View/Delete Entry".equals(action)) {
		    // Invoke SecondServlet's job here.
			response.sendRedirect("View.jsp");
		}else if ("View Reports".equals(action)) {
		    // Invoke SecondServlet's job here.
			response.sendRedirect("Reports.jsp");
		}

        
        }else{  
        		out.println("<script type=\"text/javascript\">");
        		out.println("alert('Please login first');");
        		out.println("location='index.jsp';");
        		out.println("</script>");
        }
	        out.close();
		}
	}


